<!--
 * @Descripttion:
 * @version: 1.0.0
 * @Author: htang
 * @Date: 2023-11-06 17:28:36
 * @LastEditors: htang
 * @LastEditTime: 2024-01-22 21:11:30
-->
<template>
  <div class="jeecg-basic-table alarm-lists-view">
    <div class="tip">
      <span>报警列表(记录)</span>
    </div>
    <div class="jeecg-search-layer">
      <a-form ref="form" :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="报警时间：">
              <a-range-picker
                v-model:value="alarmDateTime"
                format="YYYY-MM-DD HH:mm:ss"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                show-time
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align: right">
            <a-form-item>
              <a-button type="primary" @click="onSearch">查询</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="jeecg-table-view">
      <a-table :loading="loading" rowKey="id" :dataSource="dataSource" :columns="columns" :pagination="false">
        <!-- <template v-slot:bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex == 'action'">
            <a @click="onDetail(record)">查看</a>
            <template v-if="weightAlarmAssgin">
              <a-divider type="vertical" />
              <a @click="onUpdate(record)">修改</a>
            </template>
            <a-divider type="vertical" />
            <a style="color: red" @click="onDelete(record)">删除</a>
          </template>
        </template> -->
      </a-table>
      <Pagination
        v-model:current="ipagination.current"
        :pageSize="ipagination.pageSize"
        :total="ipagination.total"
        show-less-items
        @change="handlePageChange"
        :hideOnSinglePage="true"
      />
    </div>
    <AlarmModal ref="alarmModal" />
    <FormDrawer ref="formDrawer" @oks="init" />
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, getCurrentInstance, nextTick } from 'vue';
import { usePermission } from '/@/hooks/web/usePermission';
import { render } from '/@/utils/common/renderUtils';
import { useUserStore } from '/@/store/modules/user';
import AlarmModal from './components/detail.vue';
import { alarmPageApi, alarmDeleteApi } from '/@/api/alarm/index';
import { Pagination } from 'ant-design-vue';
import FormDrawer from './components/form.vue';

const userStore = useUserStore();

const { hasPermission } = usePermission();

const weightAlarmAssgin = computed(() => {
  return hasPermission('alarm:assign');
});

let { proxy } = getCurrentInstance();

let loading = ref(false);

let queryParam = ref({
  // 点位名称
  name: '',
  // 点位类型
  type: '',
  // 报警时间
  state: '',
  // 是否处理
  handle: '',
});

// 报警时间
let alarmDateTime = ref([]);

let timeData = ref('');

let ipagination = ref({
  current: 1,
  pageSize: 10,
  pageSizeOptions: ['10', '20', '30'],
  showTotal: (total, range) => {
    return range[0] + '-' + range[1] + ' 共' + total + '条';
  },
  showQuickJumper: true,
  showSizeChanger: true,
  total: 0,
});

// 数据源
let dataSource = ref([]);
// 字段
let columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    key: 'id',
    width: 60,
    align: 'center',
    customRender: (t, r, index) => {
      return (ipagination.value.current - 1) * ipagination.value.pageSize + t.renderIndex + 1;
    },
  },
  {
    title: '站点编码',
    align: 'center',
    dataIndex: 'stationCode',
    customRender: ({ text }) => {
      let data = userStore.getStationList.find((_) => _.stationCode == text);
      if (data) {
        return data.name;
      } else {
        return '-';
      }
    },
  },
  {
    title: '设备编码',
    align: 'center',
    dataIndex: 'equipmentCode',
  },
  {
    title: '时间',
    align: 'center',
    dataIndex: 'createTime',
  },
  {
    title: '报警类型',
    align: 'center',
    dataIndex: 'alarmType',
    customRender: ({ text }) => {
      return render.renderDict(text, 'alarm_type');
    },
  },
  {
    title: '状态',
    align: 'center',
    dataIndex: 'status',
    customRender: ({ text }) => {
      return render.renderDict(text, 'alarm_status');
    },
  },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
  },
]);

/**
 * 初始化数据列表
 */
function init() {
  let { current, pageSize } = ipagination.value;
  let data = {
    current,
    size: pageSize,
  };
  loading.value = true;
  let keys = Object.keys(queryParam.value);
  Object.values(queryParam.value).map((_, i) => {
    if (_ !== '') {
      data[keys[i]] = _;
    }
  });
  alarmPageApi(data).then((res) => {
    loading.value = false;
    let { records, size, current, total } = res;
    Object.assign(ipagination.value, {
      current,
      pageSize: size,
      total,
    });
    dataSource.value = [
      {
        stationCode: '47DE555C93',
        equipmentCode: 'TEST_EQUIP_CODE_2',
        createTime: '2024-01-15',
        alarmType: '设备故障',
        status: '已修复',
      },
    ];
  });
}

init();

/**
 * 切换当前页数
 */
function handlePageChange(page) {
  ipagination.value.current = page;
  init();
}

/**
 * 打开新增弹窗
 */
function onAdd() {
  proxy.$refs.formDrawer.visible = true;
}

/**
 * 查看详情
 */
function onDetail({ id }) {
  proxy.$refs.alarmModal.visible = true;
  nextTick(() => {
    proxy.$refs.alarmModal.init(id, false);
  });
}

/**
 * 更新告警数据
 */
function onUpdate({ id }) {
  proxy.$refs.alarmModal.visible = true;
  nextTick(() => {
    proxy.$refs.alarmModal.init(id, true);
  });
}

/**
 * 搜索数据
 */
function onSearch() {
  ipagination.value.current = 1;
  init();
}

/**
 * 删除数据
 */
function onDelete({ id }) {
  alarmDeleteApi({ id }).then((res) => {
    init();
  });
}
</script>

<style lang="less" scoped>
.alarm-lists-view {
  margin: 12px;
  background: #fff;
  .tip {
    width: 100%;
    height: 44px;
    padding: 0 0 0 43.3px;
    background: url('@/assets/images/img-17.png') no-repeat;
    background-size: 100% 100%;
    line-height: 44px;
    span {
      font-size: 16px;
      color: #fff;
    }
  }
  .jeecg-search-layer {
    margin: 0 0 17px;
    padding: 12px 15px;
    background: #fff;
    border-radius: 8px;
    .ant-form {
      max-width: auto;
      .ant-form-item {
        margin-bottom: 24px;
      }
      :deep(.ant-col) {
        width: auto;
      }
    }
  }
}
</style>